/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ecom.session;

import ecom.dao.CountryDAO;
//import ecom.dao.CurrencyDAO;
import ecom.entity.Country;
import ecom.entity.Currency;
import ecom.entity.Customer;
import javax.ejb.Stateless;

/**
 *
 * @author simon
 */
@Stateless
public class EcomEuroConverterBean implements EcomEuroConverterLocal {

    private Currency currentCurrency;

    public EcomEuroConverterBean() {
        //CurrencyDAO currencyDAO = new CurrencyDAO();
        //this.currentCurrency = currencyDAO.findByName("EUR"); // Default
    }
    
    /**
     * Permet de convertir les prix
     *   - on considère la première adresse du client
     *   - sinon on prend le Local
     * @param price
     * @param customer
     * @param country
     * @return 
     */
    @Override
    public double getPriceByCurrency(double price, Customer customer, String local) {

        CountryDAO countryDAO = new CountryDAO();
        
        if( null != customer ) {
            // on recupere l'adresse principale du client si celui ci est connecté
            this.currentCurrency = customer.getAdressList().get(0).getCountry().getCurrency();
        } else {
            Country country = countryDAO.findByLocalIso(local);
            this.currentCurrency = country.getCurrency();
        }
        
        return price * Double.parseDouble(this.currentCurrency.getRate());
    }
    
    @Override
    public String getSymbol() {
        return currentCurrency.getName();
    }
}
